package com.now.printer.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;

import com.now.printer.R;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;


/**
 * 封装ToolBar控件
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NBToolBar extends Toolbar {
    /**
     * 左侧Title
     */
    private ImageButton btnImgLeft;
    /**
     * 中间Title
     */
    private TextView mTxtMiddleTitle;
    /**
     * 右侧Title
     */
    private TextView mTxtRightTitle;

    private ImageView imgRightSearch;

    public NBToolBar(Context context) {
        this(context, null);
    }

    public NBToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NBToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.item_toolbar_title, this);
        btnImgLeft = (ImageButton) findViewById(R.id.btn_img_left);
        mTxtMiddleTitle = (TextView) findViewById(R.id.txt_main_title);
        mTxtRightTitle = (TextView) findViewById(R.id.txt_right_title);
        imgRightSearch = (ImageView) findViewById(R.id.img_right_search);
    }

    /**
     * 设置中间title的内容
     * @param text
     */
    public void setCenterTitle(String text) {
        this.setTitle(" ");
        mTxtMiddleTitle.setVisibility(View.VISIBLE);
        mTxtMiddleTitle.setText(text);
    }


    /**
     * 显示搜索按钮
     */
    public void setShowSearch() {
        imgRightSearch.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏右边按钮
     */
    public void setHideRightText() {
        mTxtRightTitle.setVisibility(View.GONE);
    }

    public void setShowRightText() {
        mTxtRightTitle.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏左边和右边的按钮
     */
    public void setToolbarLeft(OnClickListener onClickListener) {
        btnImgLeft.setImageResource(R.drawable.ic_action_menu);
        btnImgLeft.setOnClickListener(onClickListener);
    }

    /**
     * 设置中间title的内容文字的颜色
     *
     * @param color
     */
    public void setMainTitleColor(int color) {
        mTxtMiddleTitle.setTextColor(color);
    }

    /**
     * 设置title左边点击事件
     *
     * @param onClickListener
     */
    public void setBackClickListener(View.OnClickListener onClickListener) {
        btnImgLeft.setOnClickListener(onClickListener);
    }

    /**
     * 搜索
     *
     * @param onClickListener
     */
    public void setSearchClickListener(View.OnClickListener onClickListener) {
        imgRightSearch.setOnClickListener(onClickListener);
    }


    /**
     * 设置title右边文字
     *
     * @param text
     */
    public void setRightTitleText(String text) {
        mTxtRightTitle.setVisibility(View.VISIBLE);
        mTxtRightTitle.setText(text);
    }

    public String getRightTitleText() {
        return mTxtRightTitle.getText().toString();
    }

    /**
     * 设置title右边文字颜色
     *
     * @param color
     */
    public void setRightTitleColor(int color) {
        mTxtRightTitle.setTextColor(color);
    }

    /**
     * 设置title右边图标
     *
     * @param res
     */
    public void setRightTitleDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtRightTitle.setCompoundDrawables(null, null, dwRight, null);
    }

    /**
     * 设置title右边点击事件
     *
     * @param onClickListener
     */
    public void setHandlerClickListener(View.OnClickListener onClickListener) {
        mTxtRightTitle.setOnClickListener(onClickListener);
    }
}
